package approve.action;

import java.util.List;

import model.pojo.Employee;
import resource.pojo.Approvedoc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import approval.dao.ApproveDocumentDAO;

public class My_App_SavedDocument extends Activity {
	String database = "admin";
	Bundle language=null;
	String lang = "EN";
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
			lang=extras.getString("lang");
			language=extras.getBundle(lang);
		}
		return database;
	}
	
	Approvedoc AppDoc = null;
	Employee emp = null;
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_app_saveddocument);
		getDatabase();
		
		emp = (Employee) getIntent().getExtras().get("em");
		list = (ListView) findViewById(R.id.lst_mysavedocument);
		List<Approvedoc> listDocs=getListSavedDocument();
		if(language!=null){
        	TextView la_headerTitle=(TextView)findViewById(R.id.la_headerTitle);
        	la_headerTitle.setText(language.getString("saved-document")+" ("+listDocs.size()+"):");
        }
		if(listDocs.size()>0){
			list.setAdapter(new ArrayAdapter<Approvedoc>(this,
					android.R.layout.simple_list_item_1, listDocs));
			list.setOnItemClickListener(newDocument);
		}else{
			TextView empltyList=(TextView) findViewById(R.id.empltyList);
			if(language!=null){
				empltyList.setText(language.getString("no_data"));
		}
		}
		

	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	        // do something on back.
			Intent intent=new Intent(My_App_SavedDocument.this,MainFrame.class);
			intent.putExtras(getIntent().getExtras());
			startActivityForResult(intent, 0);
			
	        return true;
	    }

		return super.onKeyDown(keyCode, event);
	}

	private OnItemClickListener newDocument = new OnItemClickListener() {

		public void onItemClick(android.widget.AdapterView<?> listDoc,
				android.view.View arg1, int arg2, long arg3) {
			Approvedoc appDoc = (Approvedoc) listDoc.getItemAtPosition(arg2);

			if (appDoc.getiDTypeDoc() == 11) {
				Intent myIntent = new Intent(My_App_SavedDocument.this,
						My_MeetingReport11.class);
				myIntent.putExtras(getIntent().getExtras());
				myIntent.putExtra("appDoc", appDoc);
				startActivityForResult(myIntent, 0);

			} else if (appDoc.getiDTypeDoc() == 12) {
				Intent myIntent = new Intent(My_App_SavedDocument.this,
						My_DocumentWorkReport12.class);
				myIntent.putExtras(getIntent().getExtras());
				myIntent.putExtra("appDoc", appDoc);
				startActivityForResult(myIntent, 0);

			} else if (appDoc.getiDTypeDoc() == 16) {
				Intent myIntent = new Intent(My_App_SavedDocument.this,
						My_NewFormWeeklyReport16.class);
				myIntent.putExtras(getIntent().getExtras());
				myIntent.putExtra("appDoc", appDoc);
				startActivityForResult(myIntent, 0);
			} else {
				Intent myIntent = new Intent(My_App_SavedDocument.this,
						My_DailyReport14.class);
				myIntent.putExtras(getIntent().getExtras());
				myIntent.putExtra("appDoc", appDoc);
				startActivityForResult(myIntent, 0);
			}
		};
	};

	public List<Approvedoc> getListSavedDocument() {
		return ApproveDocumentDAO.listStorage(emp.getIdemployee(),
				getDatabase());
	}

}
